package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;
import com.wbao.dianniu.data.JobListData;
import com.wbao.dianniu.utils.DateUtils;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

/**
 * 求职列表
 *
 * @author yejianjun
 */
public class JobListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<JobListData> mList;
    private Context mContext;

    public JobListAdapter(Context context) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
    }

    public void setData(List<JobListData> list) {
        mList = list;
    }

    public void addData(List<JobListData> list) {
        if (null == mList) {
            mList = new ArrayList<JobListData>();
        }
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public List<JobListData> getList() {
        return mList;
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public void addTopData(JobListData data) {
        mList.add(0, data);
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.job_list_item, null);
            holder = new GridHolder();
            holder.headIV = convertView.findViewById(R.id.job_list_item_head);
            holder.nameTV = convertView.findViewById(R.id.job_list_item_name);
            holder.subjectTV = convertView.findViewById(R.id.job_list_item_subject);
            holder.salaryTV = convertView.findViewById(R.id.job_list_item_salary);
            holder.sexTV = convertView.findViewById(R.id.job_list_item_sex);
            holder.educationTV = convertView.findViewById(R.id.job_list_item_education);
            holder.experienceTV = convertView.findViewById(R.id.job_list_item_experience);
            holder.addressTV = convertView.findViewById(R.id.job_list_item_address);
            holder.timeTV = convertView.findViewById(R.id.job_list_item_time);
            convertView.setTag(holder);
        } else {
            holder = (GridHolder) convertView.getTag();
        }

        JobListData data = mList.get(position);
        if (null != data) {
            if (null == data.getUserInfo()) {
                holder.nameTV.setText(data.getName() == null ? "" : data.getName());
                if (null != data.getSex() && data.getSex().equals("0")) {
                    displayDefaultHeadImg(mContext, holder.headIV, R.drawable.head_girl_icon);
                    holder.sexTV.setText(mContext.getResources().getString(R.string.girl));
                } else {
                    displayDefaultHeadImg(mContext, holder.headIV, R.drawable.head_boy_icon);
                    holder.sexTV.setText(mContext.getResources().getString(R.string.boy));
                }
            } else {
                PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.getUserInfo().headPic);
                holder.nameTV.setText(data.getUserInfo().realName == null ? "" : data.getUserInfo().realName);
                if (1 == data.getUserInfo().sex) {
                    holder.sexTV.setText(mContext.getResources().getString(R.string.boy));
                } else {
                    holder.sexTV.setText(mContext.getResources().getString(R.string.girl));
                }
            }
            holder.subjectTV.setText(data.getPost() == null ? "" : data.getPost());
            holder.salaryTV.setText(data.getSalary() == null ? "" : "" + data.getSalary());
            holder.educationTV.setText(data.getEducation() == null ? "" : data.getEducation());
            holder.experienceTV.setText(data.getExperience() == null ? "" : data.getExperience());
            holder.addressTV.setText(data.getAddress() == null ? "" : "" + data.getAddress());
            holder.timeTV.setText(DateUtils.getFormatChatTime(data.getLastUpdateDate()));
        }

        return convertView;
    }

    private void displayDefaultHeadImg(Context context, ImageView imageView, int imgId) {
        if(null != context){
            Glide.with(context)
                    .load(imgId)
                    .centerCrop()
                    .placeholder(R.drawable.default_head_icon)
                    .crossFade()
                    .into(imageView);
        }
    }


    private class GridHolder/* extends UserInfoHolder */ {
        public ImageView headIV;
        public TextView nameTV;
        public TextView subjectTV;
        public TextView salaryTV;
        public TextView sexTV;
        public TextView educationTV;
        public TextView experienceTV;
        public TextView addressTV;
        public TextView timeTV;
    }

}
